package com.hj.system.system.controller;

import com.hj.system.base.config.Constant;
import com.hj.system.common.controller.BaseController;
import com.hj.system.base.domain.Tree;
import com.hj.system.base.utils.ResponseUtil;
import com.hj.system.system.domain.DeptDO;
import com.hj.system.system.service.DeptService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理
 *
 * @author wxf
 */

@Controller
@RequestMapping("/system/sysDept")
public class DeptController extends BaseController {
    private String prefix = "system/dept";
    @Autowired
    private DeptService sysDeptService;

    @GetMapping()
    @RequiresPermissions("system:sysDept:sysDept")
    public String dept() {
        return prefix + "/dept";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:sysDept:sysDept")
    public List<DeptDO> list() {
        Map<String, Object> query = new HashMap<>();
        List<DeptDO> sysDeptList = sysDeptService.list(query);
        return sysDeptList;
    }

    @GetMapping("/modify")
    @RequiresPermissions(value = {"system:sysDept:add", "system:sysDept:edit"}, logical = Logical.OR)
    public String modify(String option, String id, Model model) {
        String parentId;
        if ("add".equals(option)) {
            parentId =id;
            model.addAttribute("sysDept", new DeptDO());
        } else {
            DeptDO sysDept = sysDeptService.get(id);
            parentId=sysDept.getParentId();
            model.addAttribute("sysDept", sysDept);
        }
        model.addAttribute("parentId", parentId);
        if ("0".equalsIgnoreCase(parentId)) {
            model.addAttribute("parentDeptName", Constant.DEPT_ROOT_NAME);
        } else {
            model.addAttribute("parentDeptName", sysDeptService.get(parentId).getName());
        }
        return prefix + "/modify";
    }
    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:sysDept:add")
    public ResponseUtil save(DeptDO sysDept) {
        if (sysDeptService.save(sysDept) > 0) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:sysDept:edit")
    public ResponseUtil update(DeptDO sysDept) {
        if (sysDeptService.update(sysDept) > 0) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.error();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:sysDept:remove")
    public ResponseUtil remove(String deptId) {
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", deptId);
        if (sysDeptService.count(map) > 0) {
            return ResponseUtil.error(1, "包含下级部门,不允许修改");
        }
        if (sysDeptService.checkDeptHasUser(deptId)) {
            if (sysDeptService.remove(deptId) > 0) {
                return ResponseUtil.ok();
            }
        } else {
            return ResponseUtil.error(1, "部门包含用户,不允许修改");
        }
        return ResponseUtil.error();
    }

    @GetMapping("/tree")
    @ResponseBody
    public Tree<DeptDO> tree() {
        Tree<DeptDO> tree = sysDeptService.getTree();
        return tree;
    }


}
